package com.nervenets.web.controller;

import com.nervenets.utils.PropertiesLoader;
import com.nervenets.utils.StringUtils;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.entity.Result;
import com.nervenets.web.hibernate.dao.UserDao;
import com.nervenets.web.i18n.Translator;
import com.nervenets.web.rabbitmq.MessageProducer;
import com.nervenets.web.redis.GlobalService;
import com.nervenets.web.redis.GlobalVariablesService;
import com.nervenets.web.redis.utils.RedisSyncLocker;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

/**
 * Created by Joe
 * Date: 13-2-21 下午9:20
 * Email: joesupper@vip.qq.com
 */
public abstract class APIController {
    @Resource
    MessageProducer messageProducer;
    @Resource
    UserDao userDao;
    @Resource
    GlobalService globalService;
    @Resource
    GlobalVariablesService globalVariablesService;
    @Resource
    RedisSyncLocker redisSyncLocker;
    @Resource
    PropertiesLoader configProperties;

    protected abstract String version();

    protected String successMessage() {
        return successMessage(null);
    }

    protected String successMessage(Object object) {
        Result result = Result.success();
        result.setCode(MessageCode.code_200);
        result.setData(object);
        result.setVersion(version());
        result.setMessage(Translator.translate("code_200"));
        return StringUtils.toJson(result);
    }

    protected String errorMessage(int messageCode) {
        return errorMessage(messageCode, null);
    }

    protected String errorMessage(int messageCode, Object object) {
        Result result = Result.failed();
        result.setCode(messageCode);
        result.setData(object);
        result.setVersion(version());
        result.setMessage(Translator.translate("code_" + messageCode));
        return StringUtils.toJson(result);
    }

    protected ModelAndView message(String resource) {
        return new ModelAndView("result", "message", Translator.translate(resource));
    }
}
